package com.zx.BookSeaReader.ui.booklistdetail;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;

import com.zx.BookSeaReader.R;
import com.zx.BookSeaReader.base.BasePresenter;
import com.zx.BookSeaReader.common.APPCONST;
import com.zx.BookSeaReader.creator.DialogCreator;
import com.zx.BookSeaReader.greendao.entity.Book;
import com.zx.BookSeaReader.greendao.entity.BookCommentBean;
import com.zx.BookSeaReader.greendao.entity.BookListBean;
import com.zx.BookSeaReader.greendao.entity.UserBean;
import com.zx.BookSeaReader.greendao.service.BookListService;
import com.zx.BookSeaReader.greendao.service.BookService;
import com.zx.BookSeaReader.greendao.service.CommentService;
import com.zx.BookSeaReader.ui.bookinfo.BookInfoActivity;
import com.zx.BookSeaReader.util.Constant;
import com.zx.BookSeaReader.util.StringUtils;
import com.zx.BookSeaReader.util.TextHelper;
import com.zx.BookSeaReader.widget.itemdecoration.DividerItemDecoration;

import java.util.List;

import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;

/**
 * @Author: ZX
 * @Time: 2020/4/21
 * @Description: 功能描述
 */
public class BookListDetailPresenter implements BasePresenter {

    private BookListDetailActivity mBookListDetailActivity;
    private BookService mBookService;
    private BookListService mBookListService;
    private CommentService mCommentService;
    private UserBean userBean;
    private boolean isSubscribe;
    private boolean isMine;
    private BookListBean bookListBean;
    private List<Book> ListBooks;
    private List<BookCommentBean> ListBookComments;
    private List<BookCommentBean> ListBookHotComments;
    private BookListCommentAdapter mBookListCommentAdapter;
    private BookListHotCommentAdapter mBookListHotCommentAdapter;
    private ListBooksAdapter mListBooksAdapter;




    public BookListDetailPresenter(BookListDetailActivity bookListDetailActivity) {
        mBookListDetailActivity = bookListDetailActivity;
        mBookListCommentAdapter = new BookListCommentAdapter();
        mBookListHotCommentAdapter = new BookListHotCommentAdapter();
        mListBooksAdapter = new ListBooksAdapter();
        mBookService=new BookService();
        mCommentService=new CommentService();
        mBookListService=new BookListService();
    }

    @Override
    public void start() {
        userBean = (UserBean)mBookListDetailActivity.getIntent().getSerializableExtra(APPCONST.USER);
        bookListBean = (BookListBean)mBookListDetailActivity.getIntent().getSerializableExtra(APPCONST.BOOKLIST);
        ListBooks = mBookListService.getBookListBooks(bookListBean);
        isSubscribe = mBookListService.isBookListSub(bookListBean,userBean);

        if(bookListBean.getUsername().equals(userBean.getUsername())){
            mBookListDetailActivity.getTvSubscribe().setVisibility(View.GONE);
           // TextHelper.showText("书单是你的");
            mBookListDetailActivity.getTvEditDesc().setVisibility(View.VISIBLE);
            mBookListDetailActivity.getTvEditTitle().setVisibility(View.VISIBLE);
        }else {
          //  TextHelper.showText("书单不不是你的");
            mBookListDetailActivity.getTvSubscribe().setVisibility(View.VISIBLE);
            mBookListDetailActivity.getTvEditDesc().setVisibility(View.GONE);
            mBookListDetailActivity.getTvEditTitle().setVisibility(View.GONE);
        }
        mBookListDetailActivity.getRlRefresh().setBackground(ContextCompat.getDrawable(mBookListDetailActivity, R.color.white));
        // mBookListDetailActivity.getRlRefresh().setVisibility(View.GONE);
        mBookListDetailActivity.getRlRefresh().setVisibility(View.VISIBLE);
        mBookListDetailActivity.getRlRefresh().showFinish();

        mBookListDetailActivity.getRvBooks().setLayoutManager(new LinearLayoutManager(mBookListDetailActivity));
        mBookListDetailActivity.getRvBooks().addItemDecoration(new DividerItemDecoration(mBookListDetailActivity));
        mBookListDetailActivity.getRvBooks().setAdapter(mListBooksAdapter);
        mListBooksAdapter.refreshItems(ListBooks);

        mBookListDetailActivity.getRvComments().setLayoutManager(new LinearLayoutManager(mBookListDetailActivity));
        mBookListDetailActivity.getRvComments().addItemDecoration(new DividerItemDecoration(mBookListDetailActivity));
        mBookListDetailActivity.getRvComments().setAdapter(mBookListCommentAdapter);

        mBookListDetailActivity.getRvHotComments().setLayoutManager(new LinearLayoutManager(mBookListDetailActivity));
        mBookListDetailActivity.getRvHotComments().addItemDecoration(new DividerItemDecoration(mBookListDetailActivity));
        mBookListDetailActivity.getRvHotComments().setAdapter(mBookListHotCommentAdapter);

        mBookListDetailActivity.getTvAuthor().setText(bookListBean.getUsername());
        mBookListDetailActivity.getTvBookListTitle().setText(bookListBean.getTitle());
        mBookListDetailActivity.getTvBookListDesc().setText(bookListBean.getDesc());
        mBookListDetailActivity.getllTitleBack().setOnClickListener(view -> mBookListDetailActivity.finish());
        mBookListDetailActivity.getTvTitle().setText("书单贴详情");
        mBookListDetailActivity.getTvCreater().setText("创建自");
        if(isSubscribe){
            mBookListDetailActivity.getTvSubscribe().setText("已订阅");
        }else {
            mBookListDetailActivity.getTvSubscribe().setText("订阅");
        }

        init();


    }
    public void initComments(){
        ListBookComments = mCommentService.getBookListComments(bookListBean);
        ListBookHotComments = mCommentService.getBookListHotComments(bookListBean);
        mBookListCommentAdapter.refreshItems(ListBookComments);
        mBookListHotCommentAdapter.refreshItems(ListBookHotComments);
        if(ListBookComments.size()<1){
            mBookListDetailActivity.getRvHotComments().setVisibility(View.GONE);
            mBookListDetailActivity.getTvBestComment().setVisibility(View.GONE);
            mBookListDetailActivity.getRvComments().setVisibility(View.GONE);
            mBookListDetailActivity.getTvCommentCount().setText("共有0条评论");

        }else{
            mBookListDetailActivity.getRvHotComments().setVisibility(View.VISIBLE);
            mBookListDetailActivity.getTvBestComment().setVisibility(View.VISIBLE);
            mBookListDetailActivity.getRvComments().setVisibility(View.VISIBLE);
            mBookListDetailActivity.getTvCommentCount().setText("共有"+ListBookComments.size()+"条评论");
        }


    }
    public void init(){
        initComments();
        mListBooksAdapter.setOnItemClickListener(
                (v, pos) -> {
                    Intent intent = new Intent(mBookListDetailActivity, BookInfoActivity.class);
                    intent.putExtra(APPCONST.BOOK, mListBooksAdapter.getItem(pos));
                    intent.putExtra(APPCONST.USER, userBean);
                    mBookListDetailActivity.startActivity(intent);

                }
        );
        mBookListHotCommentAdapter.setOnItemLongClickListener(
                (v, pos) -> {
                    openItemDialog(mBookListHotCommentAdapter.getItem(pos));
                    return true;
                }
        );
        mBookListCommentAdapter.setOnItemLongClickListener(
                (v, pos) -> {
                    openItemDialog(mBookListCommentAdapter.getItem(pos));
                    return true;
                }
        );

        mBookListDetailActivity.getTvAddComment().setOnClickListener(view ->{
            addComment();
        });

        mBookListDetailActivity.getTvSubscribe().setOnClickListener(view ->{
            if(isSubscribe){
                showDialog();
            }else{
                mBookListService.addSubscribeBookList(bookListBean,userBean);
                TextHelper.showText("订阅成功");
                isSubscribe=true;
                mBookListDetailActivity.getTvSubscribe().setText("已订阅");

            }

        });
        mBookListDetailActivity.getTvEditTitle().setOnClickListener(view ->{
            editTitle();

        });
        mBookListDetailActivity.getTvEditDesc().setOnClickListener(view ->{
            editDesc();

        });

    }

    private void showDialog(){
        DialogCreator.createCommonDialog(mBookListDetailActivity, "取消订阅", "是否不再订阅此书单？",
                true, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        mBookListService.deleteSubBookList(bookListBean,userBean);
                        TextHelper.showText("取消订阅成功");
                        isSubscribe=false;
                        mBookListDetailActivity.getTvSubscribe().setText("订阅");
                        dialogInterface.dismiss();
                    }
                }, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        dialogInterface.dismiss();
                    }
                });
    }


    /**
     * 添加评论
     * @param
     */
    private void addComment(){
        final EditText editText = new EditText(mBookListDetailActivity);
        AlertDialog.Builder editDialog = new AlertDialog.Builder(mBookListDetailActivity);
        //editDialog.setIcon(R.mipmap.ic_launcher_round);
        editDialog.setTitle("输入评论内容").setView(editText);
        editDialog.setPositiveButton("发送", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String content = editText.getText().toString();
                int size = ListBookComments.size()+1;
                mCommentService.addBookListComment(bookListBean,userBean,content,size);
                TextHelper.showText("添加评论成功");
                initComments();
            }
        });
        editDialog.show();
    }
    /**
     * 编辑标题
     * @param
     */
    private void editTitle(){
        final EditText editText = new EditText(mBookListDetailActivity);
        AlertDialog.Builder editDialog = new AlertDialog.Builder(mBookListDetailActivity);
        //editDialog.setIcon(R.mipmap.ic_launcher_round);
        editDialog.setTitle("输入标题").setView(editText);
        editDialog.setPositiveButton("修改", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String content = editText.getText().toString();
                boolean check = mBookListService.existBookList(userBean, content);
                if (check) {
                    TextHelper.showText("你已经创建过同名的书单贴！");
                } else {
                    bookListBean.setTitle(content);
                    mBookListService.updateEntity(bookListBean);
                    mBookListDetailActivity.getTvBookListTitle().setText(bookListBean.getTitle());
                    TextHelper.showText("修改标题成功");
                }

            }
        });
        editDialog.show();
    }

    /**
     * 编辑简介
     * @param
     */
    private void editDesc(){
        final EditText editText = new EditText(mBookListDetailActivity);
        AlertDialog.Builder editDialog = new AlertDialog.Builder(mBookListDetailActivity);
        //editDialog.setIcon(R.mipmap.ic_launcher_round);
        editDialog.setTitle("输入简介").setView(editText);
        editDialog.setPositiveButton("修改", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String content = editText.getText().toString();
                bookListBean.setDesc(content);
                mBookListService.updateEntity(bookListBean);
                mBookListDetailActivity.getTvBookListDesc().setText(bookListBean.getDesc());
                TextHelper.showText("修改简介成功");
            }
        });
        editDialog.show();
    }

    private void openItemDialog(BookCommentBean commentBean) {
        String[] menus;
        menus = mBookListDetailActivity.getResources().getStringArray(R.array.menu_long_comment_click);
        AlertDialog commentBeanDialog = new AlertDialog.Builder(mBookListDetailActivity)
                .setTitle(commentBean.getContent())
                .setAdapter(new ArrayAdapter<String>(mBookListDetailActivity,
                                android.R.layout.simple_list_item_1, menus),
                        (dialog, which) -> onItemMenuClick(menus[which], commentBean))
                .setNegativeButton(null, null)
                .setPositiveButton(null, null)
                .create();

        commentBeanDialog.show();
    }

    private void onItemMenuClick(String which, BookCommentBean commentBean) {
        switch (which) {
            //点赞
            case "点赞":
                int likeCount = commentBean.getLikeCount()+1;
                commentBean.setLikeCount(likeCount);
                mCommentService.updateEntity(commentBean);
                TextHelper.showText("点赞成功");
                initComments();

                break;
            //删除
            case "删除":
                if(userBean.getAdmin()!=1){
                    TextHelper.showLongText("你不是管理员无法删除评论");
                }else{
                    mCommentService.deleteEntity(commentBean);
                    initComments();
                }
                break;
            default:
                break;
        }
    }
}
